﻿using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.DatabaseServices;

/**
 * 单行文字 
 */
namespace Dmo_lc_2.Shape
{
    public class RotatedDimensionShape : BaseShape
    {
        public string textString;
        public double[] position;
        public double height;
        public int horizontalMode;
        public int verticalMode;
        public int justify;
        public double rotation;
        public double widthFactor;

        public double[] xLine1Point; // 标注原点1
        public double[] xLine2Point;// 标注原点2
        public double[] textPosition;// 标注文字位置
        public double textRotation;// 标注文字旋转
        public double dimTxt;// 标注文字高度

        public double dimExe;// 超出尺寸线距离
        public double dimExo;// 尺寸线距离两端原点的距离

        public int[] dimClrd; // 尺寸线颜色
        public int[] dimClre; // 尺寸边界线颜色
        public int[] dimClrt; // 标注文字颜色
        public double dimaSz; // 标注箭头高度

        public double[] normal;
        public int dimDec;

        public String prefix;
        public String suffix;
        public double measurement;

        /**
         * 箭头控制
         * false:使用由 DIMBLK 系统变量设置的箭头块
           true:使用由 DIMBLK1 和 DIMBLK2 系统变量设置的箭头块
         */
        public bool dimSah;
 
        public List<BaseShape> dimBlkShapes = new List<BaseShape>();
        public List<BaseShape> dimBlk1Shapes = new List<BaseShape>();
        public List<BaseShape> dimBlk2Shapes = new List<BaseShape>();

        public string dimBlkName;
        public string dimBlk1Name;
        public string dimBlk2Name;

        public RotatedDimensionShape(Entity in_entity, Transaction tr) : base(in_entity, tr)
        {
        }

        public override void Init()
        {
            shapeType = ShapeType.ROTATEDDIMENSION;
            shapeTypeName = shapeType.ToString();

            RotatedDimension in_shape = (RotatedDimension)mEntity;

            color = getColor();

            rotation = in_shape.Rotation;
            Console.WriteLine("rotation = " + rotation);

            xLine1Point = new double[] { in_shape.XLine1Point.X, in_shape.XLine1Point.Y, in_shape.XLine1Point.Z };
            xLine2Point = new double[] { in_shape.XLine2Point.X, in_shape.XLine2Point.Y, in_shape.XLine2Point.Z };
            textPosition = new double[] { in_shape.TextPosition.X, in_shape.TextPosition.Y, in_shape.TextPosition.Z };
            dimTxt = in_shape.Dimtxt;
            dimExo = in_shape.Dimexo;
            dimExe = in_shape.Dimexe;
            
            dimClrd = new int[] { in_shape.Dimclrd.ColorValue.R, in_shape.Dimclrd.ColorValue.G, in_shape.Dimclrd.ColorValue.B };
            dimClre = new int[] { in_shape.Dimclre.ColorValue.R, in_shape.Dimclre.ColorValue.G, in_shape.Dimclre.ColorValue.B };
            dimClrt = new int[] { in_shape.Dimclrt.ColorValue.R, in_shape.Dimclrt.ColorValue.G, in_shape.Dimclrt.ColorValue.B };
            
            normal = new double[] { in_shape.Normal.X, in_shape.Normal.Y, in_shape.Normal.Z };
            dimaSz = in_shape.Dimasz;
            dimDec = in_shape.Dimdec;
            dimSah = in_shape.Dimsah;
            
            prefix = in_shape.Prefix;
            suffix = in_shape.Suffix;
            //measurement = in_shape.Measurement;
            
            ObjectId dimBlk = in_shape.Dimblk;
            ObjectId dimBlk1 = in_shape.Dimblk1;
            ObjectId dimBlk2 = in_shape.Dimblk2;
            
            if (dimSah)
            {
                dimBlk1Shapes = getDimBlkShapesByObjId(dimBlk1, mTransaction, ref dimBlk1Name);
                dimBlk2Shapes = getDimBlkShapesByObjId(dimBlk2, mTransaction, ref dimBlk2Name);
            }
            else {
                dimBlkShapes = getDimBlkShapesByObjId(dimBlk, mTransaction,ref dimBlkName);
            }
        }

        private List<BaseShape> getDimBlkShapesByObjId(ObjectId inId, Transaction inTr , ref String dimBlkStr) {
            if (inId.IsNull)
            {
                dimBlkStr = "default";
                return null;
            }

            BlockTableRecord dimBlk1Recorder = (BlockTableRecord)inTr.GetObject(inId, OpenMode.ForRead);
            List<BaseShape> result = new List<BaseShape>();

            dimBlkStr = dimBlk1Recorder.Name;
            foreach (ObjectId oid in dimBlk1Recorder)
            {
                DBObject dbobj = mTransaction.GetObject(oid, OpenMode.ForRead);
                if (dbobj is Entity)
                {
                    Entity entity = dbobj as Entity;
                    BaseShape bs = ShapeFactory.genShapeByEntity(mTransaction, dbobj);
                    if (bs != null && bs.shapeType != BaseShape.ShapeType.BASE) {
                        bs.color = dimClrd;

                    }
                    result.Add(bs);
                }
            }

            return result;
        }
    }
}